package com.renwei.yunlong.activity.me;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.InputFilter;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.CommonBean;
import com.renwei.yunlong.global.AppHelper;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.LogUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.renwei.yunlong.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.FormatUtil;
import com.zyf.fwms.commonlibrary.utils.SharedPreUtil;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 修改密码--页面
 * <p>
 * Created by Administrator on 2017/8/25.
 */

public class ChangePasswordActivity extends BaseActivity implements View.OnClickListener, HttpTaskListener {

    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_edit)
    TextView tvEdit;
    @Bind(R.id.et_original_pwd)
    EditText etOriginalPwd;//原密码
    @Bind(R.id.et_pwd)
    EditText etPwd;//设置密码
    @Bind(R.id.et_confirm_pwd)
    EditText etConfirmPwd;//确认密码

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        initData();
    }

    private void initData() {
        ivBack.setVisibility(View.VISIBLE);
        tvEdit.setVisibility(View.VISIBLE);
        tvTitle.setText("修改密码");
        ivBack.setOnClickListener(this);
        tvEdit.setOnClickListener(this);
        tvEdit.setText("保存");

        etPwd.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20), AppHelper.emojiFilter});
        etConfirmPwd.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20), AppHelper.emojiFilter});
        etOriginalPwd.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20), AppHelper.emojiFilter});
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_edit://保存
                String userId = "";
                String pwd = "";
                if ("1".equals(companyType)) {
                    userId = ownerBean.getRows().getUserId();
                    pwd = SharedPreUtil.getString(this, "owner_pwd", "");
                } else {
                    userId = serviceLoginBean.getRows().getUserId();
                    pwd = SharedPreUtil.getString(this, "service_pwd", "");
                }
                //--------------------------------------------------------------
                checkPassWord(userId, pwd);
                break;
            default:
                break;
        }
    }

    //region 请求网络修改密码
    //检查密码输入
    public void checkPassWord(String userId, String oldPwd) {
        String inputOldPwd = etOriginalPwd.getText() == null ? "" : etOriginalPwd.getText().toString();
        String newPwd = etPwd.getText() == null ? "" : etPwd.getText().toString();
        String confirmPwd = etConfirmPwd.getText() == null ? "" : etConfirmPwd.getText().toString();
        if (inputOldPwd.length() == 0) {
            showCenterInfoMsg("请输入原密码");
        } else {
            LogUtil.e("inputOldPwd:" + inputOldPwd + "oldPwd:" + oldPwd);
            if (!inputOldPwd.equals(oldPwd)) {
                showCenterInfoMsg("请输入正确的原密码 ");
            } else {
                if (newPwd.length() == 0) {
                    showCenterInfoMsg("请输入密码");
                    return;
                }
                if (confirmPwd.length() == 0) {
                    showCenterInfoMsg("请输入确认密码");
                    return;
                }
                if (!newPwd.equals(confirmPwd)) {
                    showCenterInfoMsg("请确认两次输入密码是否匹配");
                    return;
                }
                if (!FormatUtil.isPasswordFormat(confirmPwd)) {
                    showCenterInfoMsg("密码格式不正确,密码限制在6-12位数字、字母的组合");
                    return;
                } else {
                    HashMap hashMap = new HashMap();
                    hashMap.put("userId", userId);
                    hashMap.put("oldpassword", oldPwd);
                    hashMap.put("userPwd", newPwd);
                    ServiceRequestManager manager = ServiceRequestManager.getManager();
                    manager.changPassword(this, JsonMapUtil.mapToJson(hashMap), this);
                }

            }
        }
    }

    @Override
    public void onSuccess(int requestId, String result) {
        LogUtil.e("修改密码回掉？ ：：" + result);
        switch (requestId) {
            case 5:
                CommonBean ownResult = new Gson().fromJson(result, CommonBean.class);
                if (ownResult.getMessage().getCode() == 200) {//成功  200
                    showCenterSuccessMsg("修改成功");
                    exitApp();
                } else {//账号或密码错误
                    showCenterInfoMsg("账号或密码错误");
                }
                break;
            case 6:
                CommonBean serviceResult = new Gson().fromJson(result, CommonBean.class);
                if (serviceResult.getMessage().getCode() == 200) {//成功  200
                    showCenterSuccessMsg("修改成功");
                    exitApp();
                } else {//账号或密码错误
                    showCenterInfoMsg("账号或密码错误");
                }
                break;
            default:
                break;
        }
    }

    //endregion
    @Override
    public void onException(int requestId, String code) {
        showTopWrongMsg(getResources().getString(R.string.net_work_error));
    }
}
